/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.view;

import com.minierp.helper.EncCommonHelper;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.Region;
import org.springframework.web.servlet.view.document.AbstractExcelView;

public class EstBackCountExcelView extends AbstractExcelView {
	public void buildExcelDocument(Map model, HSSFWorkbook workbook,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		response.reset();
		response.setContentType("APPLICATION/vnd.ms-excel");
		response.setHeader("Content-Disposition",
				"attachment;filename=\"EstBackCount.xls\"");
		HSSFFont font = workbook.createFont();
		font.setFontHeightInPoints(10);
		font.setColor(10);
		font.setFontName("黑体");
		font.setBoldweight(700);
		HSSFCellStyle cellStyle = workbook.createCellStyle();
		cellStyle.setFont(font);
		cellStyle.setAlignment(2);
		cellStyle.setBorderBottom(1);
		cellStyle.setWrapText(true);
		cellStyle.setFillForegroundColor(22);
		cellStyle.setFillPattern(1);
		HSSFSheet sheet = workbook.createSheet("list");
		HSSFRow r = sheet.createRow(0);
		HSSFCell c = r.createCell(0);
		r.createCell(1);
		r.createCell(2);
		r.createCell(3);
		r.createCell(4);
		r.createCell(5);
		r.createCell(6);
		r.createCell(7);
		r.createCell(8);
		r.createCell(9);
		c.setCellValue(new HSSFRichTextString("回款合计：" + model.get("costTotal")
				+ "元 预付款：" + model.get("impTotal") + "元 结欠款："
				+ model.get("recTotal") + "元"));
		sheet.addMergedRegion(new Region(0, 0, 0, 9));
		r = sheet.createRow(1);
		r.createCell(0).setCellValue(new HSSFRichTextString("类别"));
		r.createCell(1).setCellValue(new HSSFRichTextString("等级"));
		r.createCell(2).setCellValue(new HSSFRichTextString("地区"));
		r.createCell(3).setCellValue(new HSSFRichTextString("名称"));
		r.createCell(4).setCellValue(new HSSFRichTextString("联系人"));
		r.createCell(5).setCellValue(new HSSFRichTextString("电话"));
		r.createCell(6).setCellValue(new HSSFRichTextString("经办人"));
		r.createCell(7).setCellValue(new HSSFRichTextString("回款金额"));
		r.createCell(8);
		r.createCell(9);
		r = sheet.createRow(2);
		r.createCell(0);
		r.createCell(1);
		r.createCell(2);
		r.createCell(3);
		r.createCell(4);
		r.createCell(5);
		r.createCell(6);
		r.createCell(7).setCellValue(new HSSFRichTextString("预付"));
		r.createCell(8).setCellValue(new HSSFRichTextString("结欠"));
		r.createCell(9).setCellValue(new HSSFRichTextString("小计"));
		sheet.addMergedRegion(new Region(1, 0, 2, 0));
		sheet.addMergedRegion(new Region(1, 1, 2, 1));
		sheet.addMergedRegion(new Region(1, 2, 2, 2));
		sheet.addMergedRegion(new Region(1, 3, 2, 3));
		sheet.addMergedRegion(new Region(1, 4, 2, 4));
		sheet.addMergedRegion(new Region(1, 5, 2, 5));
		sheet.addMergedRegion(new Region(1, 6, 2, 6));
		sheet.addMergedRegion(new Region(1, 7, 1, 9));
		List list = (List) model.get("list");
		List etList = (List) model.get("estTypeList");
		List elList = (List) model.get("estLevelList");
		List areaList = (List) model.get("areaList");

		for (int os = 0; os < list.size(); ++os) {
			Object[] objs = (Object[]) list.get(os);
			r = sheet.createRow(os + 3);
			String linkman = "";
			String linkmanPhone = "";
			String managerName = "";
			if (objs[9] != null) {
				linkman = objs[9].toString();
			}

			if (objs[10] != null) {
				linkmanPhone = objs[10].toString();
			}

			if (objs[8] != null) {
				managerName = objs[8].toString();
			}

			BigDecimal impCost = new BigDecimal(objs[0].toString());
			BigDecimal recCost = new BigDecimal(objs[1].toString());
			BigDecimal cost = new BigDecimal(objs[2].toString());
			r.createCell(0).setCellValue(
					new HSSFRichTextString(EncCommonHelper.getSimpleObjectName(
							etList, Long.parseLong(objs[6].toString()))));
			r.createCell(1).setCellValue(
					new HSSFRichTextString(EncCommonHelper.getSimpleObjectName(
							elList, Long.parseLong(objs[7].toString()))));
			r.createCell(2).setCellValue(
					new HSSFRichTextString(EncCommonHelper.getSimpleObjectName(
							areaList, Long.parseLong(objs[5].toString()))));
			r.createCell(3).setCellValue(
					new HSSFRichTextString(objs[4].toString()));
			r.createCell(4).setCellValue(new HSSFRichTextString(linkman));
			r.createCell(5).setCellValue(new HSSFRichTextString(linkmanPhone));
			r.createCell(6).setCellValue(new HSSFRichTextString(managerName));
			r.createCell(7).setCellValue(impCost.doubleValue());
			r.createCell(8).setCellValue(recCost.doubleValue());
			r.createCell(9).setCellValue(cost.doubleValue());
		}

		ServletOutputStream arg21 = response.getOutputStream();
		workbook.write(arg21);
		arg21.flush();
		arg21.close();
	}
}